package org.jet.emall.reptile.utils;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jet.emall.reptile.pojo.ImageModel;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientUtils {

    //创建httpclient连接池
    private static PoolingHttpClientConnectionManager connectionManager;
    static{
        connectionManager=new PoolingHttpClientConnectionManager();
        //定义连接池最大连接数
        connectionManager.setMaxTotal(200);
        //对指定的网址最多只有20个连接
        connectionManager.setDefaultMaxPerRoute(20);
    }

    private static CloseableHttpClient getCloseableHttpClient(){
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
        return httpClient;
    }

    private static String execute(HttpRequestBase httpRequestBase) throws IOException {
        httpRequestBase.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0");

        //设置超时时间
        RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000).setSocketTimeout(10 * 1000).build();

        httpRequestBase.setConfig(config);
        CloseableHttpClient httpClient = getCloseableHttpClient();
        CloseableHttpResponse response = httpClient.execute(httpRequestBase);

        String html = EntityUtils.toString(response.getEntity(), "utf-8");
        return html;
    }

    public static String doGet(String url) throws IOException {
        HttpGet httpGet = new HttpGet(url);
        String cookieText = "shshshfpa=b68dee5b-78c1-99d9-779a-e2c6a994255f-1581991643; shshshfpb=osA98t0LNMv8HpoE6Wy4mZw%3D%3D; unpl=V2_ZzNtbRUERUEgDk4GL0sPUGIGFlwRBxYcJQhPXCtMVAJjUxNbclRCFnQUR1FnGlgUZwYZWUtcRhBFCEdkeBBVAWMDE1VGZxBFLV0CFSNGF1wjU00zQwBBQHcJFF0uSgwDYgcaDhFTQEJ2XBVQL0oMDDdRFAhyZ0AVRQhHZH4cWARgBxpZQWdzEkU4dlJ%2fGlgBZDMTbUNnAUEpDEVVeh1bSGIGFlxFU0sRdjhHZHg%3d; __jdv=76161171|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_fb7ed79cecbd4450bad8a189ad965a07|1587797925034; __jdu=445065952; areaId=8; ipLoc-djd=8-632-635-0; PCSYCityID=CN_210000_211300_211324; __jdc=122270672; shshshfp=b3c2da450f6f3f57fa5dd96fba4621d3; __jda=122270672.445065952.1587797924.1587866140.1587870904.5; wlfstk_smdl=bqtxweudzcjsw46ejgyeba8io6hr3xwe; _pst=307833140-522147; logintype=qq; unick=307833140-522147; pin=307833140-522147; npin=307833140-522147; thor=320F22528AF174B3DAADB5DA986BF703FA33C058E28931EEEC5FCF484C8CA7ED9F8A1B8CA7FD89FDC25B07FA4511B3AB155CBF6039D96506B5745948CBC987AD524DB52386A822DFC449956398FB480D2B56ED28F7F015C8923EF62BCB927F2A4C15FD2B30D438A7A724AB73D0C124A71296867E5DE276BD81468F533D2A0320D2999D37DAAAE93ED38EFEAAE85A36ABAEA5444D89C6E797F282EE107D12FB2F; _tp=nhlcfURWqOvkFTWFuV9q37F2Dd%2BSJMaLCxMAENoJEik%3D; pinId=1eO242_5anuL1JtTdHLJF7V9-x-f3wj7; shshshsID=a27d483d46882954cc9b2e441e9b1fb4_11_1587873059462; __jdb=122270672.15.445065952|5.1587870904; 3AB9D23F7A4B3C9B=YKXOBZYEB6XCPFZAPJGURPSC3AK2BIV6MTDAI3Z5JTKRASXOGV3XK2P434FO2NAFQHY66PWX7XFI77J374FCQRNRJ4";
        String referer = "https://graph.qq.com/oauth2.0/show?which=Login&display=pc&response_type=code&state=CF094E537A307289AA4C9512980B8180A2AC86E01EB7BE3C51A7D1D799A8A860FA61B646A6CAAB30AFE2FA0CAD58307B&client_id=100273020&redirect_uri=https%3A%2F%2Fqq.jd.com%2Fnew%2Fqq%2Fcallback.action%3Fview%3Dnull%26uuid%3D98cf37a91811444791ecf1cfa174bc63";
        String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36";
        httpGet.setHeader("cookie",cookieText);
        httpGet.setHeader("referer",referer);
        httpGet.setHeader("user-agent",userAgent);
        String html = execute(httpGet);
        return html;
    }

    public static String doPost(String url, Map<String,String> params) throws IOException {
        HttpPost httpPost = new HttpPost(url);

        List<BasicNameValuePair> list = new ArrayList<>();
        for (String key : params.keySet()) {
            list.add(new BasicNameValuePair(key,params.get(key)));
        }

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list);
        httpPost.setEntity(entity);

        return execute(httpPost);
    }

    public static ImageModel doDownloadImage(String url) throws IOException{

        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0");

        //设置超时时间
        RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000).setSocketTimeout(10 * 1000).build();

        httpGet.setConfig(config);
        CloseableHttpClient httpClient = getCloseableHttpClient();
        CloseableHttpResponse response = httpClient.execute(httpGet);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        response.getEntity().writeTo(outputStream);

        ImageModel imageModel = new ImageModel();
        imageModel.setData(outputStream.toByteArray());
        String flag = ".com/";
        imageModel.setFileName(url.substring(url.indexOf(flag)+flag.length()));
        return imageModel;
    }
}
